<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>

    /* 
    区别Map与WeakMap
    1. Map的key是任意类型, WeakMap的key是对象类型
    2. 当key为对象时, 
      map中保存的key对象是强引用 => 即使外部对key对象的引用断开, key对象不被回收, 健值对也不会
      weakMap中保存的key对象是弱引用 => 当外部对key对象的引用断开, key对象全被回收, 健值对也会
    */
    const map1 = new Map()
    map1.set('abc', 'tttt')
    let obj1 = {m: 1}
    map1.set(obj1, 'kkkk')
    console.log(map1, map1.size)
    obj1 = null
    console.log(map1, map1.size)
    

    const map2 = new WeakMap()
    // map2.set('abc', 'mmm')   // 不可以是string
    let obj2 = {m: 2}
    map2.set(obj2, 'nnn')
    console.log(map2)
    obj2 = null
    console.log(map2)

  </script>
</body>
</html>